""" TODO
 a. could be done in Unix shell script?
"""

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defconstant *test-input-data* (sort '("COLIN" "PATRICIA" "LINDA" "BARBARA" "ELIZABETH" "JENNIFER") 'string<=))

(defparameter *input-data* 
  (with-open-file (f (merge-pathnames #P"p22_names.txt"))
      (loop for name = (read f nil) while name 
		  collect name)))
		  
;;; what's the difference between char-code and char-int?
(defun alphabetical-value (word &optional (cal-op '+))
  (reduce cal-op
    (map 'vector
	     (lambda (x) (1+ (- (char-int x) (char-int #\A))))
	     ;(lambda (x) (1+ (- (char-code x) (char-code #\A))))
	     (string word))))

(defun name-score (name index)
  (* index (alphabetical-value name)))

(defun p22 (&optional (data *input-data*))
  (sort data 'string<)
  (loop for i from 1 to (length data)
        summing (* (alphabetical-value (nth (1- i) data)) i)))

(format t "~a~%" (time (p22 )))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; code from euler forum
(defun alphabetical-value (word)
  (loop for c across word
        summing (- (char-int c) 64)))
 
(defun project-euler-22 ()
  (let ((names (copy-seq *input-data*)))
    (loop for name in (sort names #'string-lessp)
          for i from 1
          summing (* i (alphabetical-value name)))))

(format t "~a~%" (time (project-euler-22)))
